 <?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 
*/
class Auth extends CI_Controller
{

    public function index()
    {

    	echo "auth/base: 只能获取openid. oauth/userinfo: 获取用户信息";

    }

	public function Base() {
	
		$scope = 'snsapi_base';
		$this->OAuthThirdParty($scope);

	}

	public function UserInfo()
	{

		$scope = 'snsapi_userinfo';
		$this->OAuthThirdParty($scope);

	}
	public function UserInfoV2()
	{

		$this->load->helper('cookie');
		$scope = 'snsapi_userinfo';
		$state =  $this->input->get('state');
		$code = $this->input->get('code');

		$openid_cookie =  $this->input->cookie('openid');
		$access_token_cookie =  $this->input->cookie('access_token');

		if ($openid_cookie && $access_token_cookie) {
			echo $openid_cookie.'<br>';
			echo $access_token_cookie.'<br>';
			// return;
			$userinfo = $this->ci_wechat->getOauthUserinfo($openid_cookie, $access_token_cookie);
			
			// echo $this->ci_wechat->errCode.'<br>';
			// echo $this->ci_wechat->errMsg.'<br>';

			echo $this->ci_wechat->getOauthAuth($access_token_cookie, $openid_cookie).'<br>';
			echo json_encode($userinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).'<br>';

			echo true;
			echo false;
			return;
		}

		$json = $this->ci_wechat->getOauthAccessToken();

		if ($json) {
			set_cookie('openid', $json['openid'], $json['expires_in']);
			set_cookie('access_token', $json['access_token'], $json['expires_in']);
			// echo $json['expires_in'];
			// print_r($json['expires_in']);
			// return;
			$userinfo = $this->ci_wechat->getOauthUserinfo($json['access_token'], $json['openid']);
			echo json_encode($userinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
			return;
		}

		// 微信官方授权后重定向的回调链接地址
		$callback = current_url();

		$oauth_url =  $this->ci_wechat->getOauthRedirect($callback, $state, $scope);
		redirect($oauth_url);
	}

	public function help()
	{
		$this->load->view('auth_help');
	}

	private function OAuthThirdParty($scope = 'snsapi_base')
	{

		$state =  $this->input->get('state');
		# 授权后重定向的回调链接地址 参数 redirect_uri
		$redirect_uri = $this->input->get('redirect_uri');

		if (!$redirect_uri) {
			# code...
			echo "缺少授权后重定向的回调链接地址";
			return;
		}

		$json = $this->ci_wechat->getOauthAccessToken();

		if ($json) {
			# code...
			if ($scope === 'snsapi_base') {
				# code...
				$query_data = array('openid' => $json['openid'], 'state' => $state);
			} elseif ($scope === 'snsapi_userinfo') {
				# code...
				$userinfo = $this->ci_wechat->getOauthUserinfo($json['access_token'], $json['openid']);
				$userinfo = json_encode($userinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
				$query_data = array('info' => $userinfo, 'state' => $state);
			} else {
				# code...
				echo "应用授权作用域scope参数值,应为snsapi_base或是snsapi_userinfo";
				return;
			}

			$query_data = http_build_query($query_data);

			$join_symbol = (strstr($redirect_uri, "?")) ? '&' : '?';
			$redirect_uri = $redirect_uri.$join_symbol.$query_data;
			redirect($redirect_uri);
			return;
		}

		
		$callback = current_url();
		$callback = $callback.'?redirect_uri='.urlencode($redirect_uri);
		$oauth_url = $this->ci_wechat->getOauthRedirect($callback, $state, $scope);
		redirect($oauth_url);

	}


}
